package com.finalproject.entity;
import java.util.*;
/**
 * The cafe class maintains a collection of CafeItems. 
 * The methods allow the cafe to add and remove CafeItems, return the the number of items given the name etc.
 * @author Cagri
 *
 */
public class Cafe {
	
	private int id;
	private int[] coordinates=new int[2];
	private Map<String,CafeItem> cafeItemMap;
	private List<CafeObserver> cafeObservers = new ArrayList<CafeObserver>();
	private String name;
	public interface CafeObserver {
		public void onUpdate(Cafe cafe);
	}

	
	public Cafe(int id, int[] coordinates) {
		this.id = id;
		//cafeItemMap =  new  HashMap<String,List<CafeItem>>();
		cafeItemMap =  new HashMap<String,CafeItem>();
		this.coordinates = coordinates;
		this.cafeObservers = new ArrayList<Cafe.CafeObserver>();
		
	}
	
	public void addCafeObserver(CafeObserver cafeObserver) {
		if(cafeObservers == null) {
			cafeObservers = new ArrayList<Cafe.CafeObserver>();
		}
		if(!cafeObservers.contains(cafeObserver)) {
			cafeObservers.add(cafeObserver);
		}
		
	}
	
	public void removeCafeObserver(CafeObserver cafeObserver) {
		cafeObservers.remove(cafeObserver);
	}
	
	
	
	private void fireCafeObserver(Cafe cafe) {
		for(CafeObserver cafeObserver: cafeObservers) {
			cafeObserver.onUpdate(cafe);
		}
	}
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int[] getCoordinates() {
		return coordinates;
	}

	public void setCoordinates(int[] coordinates) {
		this.coordinates = coordinates;
	}
	
	
	
	/**
	 * adds a cafeItem
	 * @param cafeItem The item to be added
	 */
	/*public void add(CafeItem cafeItem) {
		String name = cafeItem.getName();
		List<CafeItem> cafeItems = cafeItemMap.get(name);
		if(cafeItems == null) {
			cafeItems = new ArrayList<CafeItem>();
		}
		cafeItems.add(cafeItem);
		cafeItemMap.put(name, cafeItems);
	}*/
	public void add(CafeItem cafeItem) {
		cafeItemMap.put(cafeItem.getName(), cafeItem);
	}

	/**
	 * retrieves the set that contains the names of CafeItems 
	 * @return the set of of keys in cafeItemMap
	 */
	public Set<String> getCafeItemNames() {
		return cafeItemMap.keySet();
	}
	
	/**
	 * Returns the number of the items with that given name
	 * @param name	The name of the item
	 * @return the number of the items with that given name, 0 if there is no such item
	 */
	/*
	public int getCafeItemsCountByName(String name) {
		List<CafeItem> cafeItems = cafeItemMap.get(name);
		if(cafeItems == null) {
			return 0;
		}
		return cafeItems.size();
	}*/
	
	
	/**
	 * the item with that given name
	 * @param name	The name of the item
	 * @return the item with that given name
	 */
	public CafeItem getCafeItemsByName(String name) {
		CafeItem item = cafeItemMap.get(name);
		return item;
	}


	/**
	 * Removes the items with the specified names and number form the list
	 * @param name the name of the item to be removed from the list
	 * @param howMany the number of the items to be removed with the given name
	 * @throws IllegalArgumentException if the number is less than 1
	 * @throws CafeException if there isn't enough number of items with the given name
	 */
	public CafeItem buyCafeItem(String name) throws CafeException{
		CafeItem cafeItem = cafeItemMap.get(name);
		
		
		
		fireCafeObserver(this);
		return cafeItem;
	}
	/**
	 * Removes the items with the specified names and number form the list
	 * @param name the name of the item to be removed from the list
	 * @param howMany the number of the items to be removed with the given name
	 * @throws IllegalArgumentException if the number is less than 1
	 * @throws CafeException if there isn't enough number of items with the given name
	 */
	public List<CafeItem> buyCafeItem(String name, int howmany) throws CafeException{
		if (howmany<1)
			throw new IllegalArgumentException("The number must be a positive integer");
		CafeItem item = cafeItemMap.get(name);
		List<CafeItem> buyItems = new ArrayList<CafeItem>();
		
		for (int i=0;i<howmany;i++){
			buyItems.add(item);
		}
		return buyItems;
	}
	/*public List<CafeItem> buyCafeItem(String name, int howmany) throws CafeException{
		if (howmany<1)
			throw new IllegalArgumentException("The number must be a positive integer");
		List<CafeItem> cafeItems = cafeItemMap.get(name);
		List<CafeItem> buyItems = new ArrayList<CafeItem>();
		
		if(cafeItems.size() < howmany ) {
			throw new CafeException("Insufficent CafeItems");
		}
		int count = 0;
		for(CafeItem cafeItem: cafeItems) {
			buyItems.add(cafeItem);
			cafeItems.remove(cafeItem);
			if(count == howmany) {
				break;
			}
			count++;
		}
		return buyItems;
	}*/


	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}


	public class CafeException extends Exception{
		private static final long serialVersionUID = 1L;

		public CafeException(String name) {
			super(name);
		}
	}




}
